package graph.chart;

import graph.Point;
import graph.chart.point.decorator.PointDecorator;

import java.awt.Graphics;
import java.awt.Graphics2D;


public class LineChart extends GenericChart{


	@Override
	public void paintChart(Graphics2D g2D){
		if(this.plotSet != null && plotSet.getPoints() != null && this.genericGraph != null){
			
			Point startPoint = null;
			Point endPoint = null;
			
			for(Point point : plotSet.getPoints()){
				endPoint = genericGraph.pointToScreenCoordinates(point);
				
				if(startPoint != null && endPoint != null){
					g2D.drawLine((int)startPoint.getX(), (int)startPoint.getY(), (int)endPoint.getX(), (int)endPoint.getY());
				}
				
				startPoint = endPoint;
				endPoint = null;
			}
			
			/* X axis mirror - if enabled*/
			if(plotSet.isxAxisMirrorEnabled()){
				startPoint = null;
				endPoint = null;
				for(Point point : plotSet.getPoints()){
					Point mirror = new Point(point.getX(), -point.getY());
					endPoint = genericGraph.pointToScreenCoordinates(mirror);
					
					if(startPoint != null && endPoint != null){
						g2D.drawLine((int)startPoint.getX(), (int)startPoint.getY(), (int)endPoint.getX(), (int)endPoint.getY());
					}
					
					startPoint = endPoint;
					endPoint = null;
				}
			}
			
			/* decorate points */
			if(plotSet.getPointDecorator() != null){
				PointDecorator decorator = plotSet.getPointDecorator();
				for(Point point : plotSet.getPoints()){
					Point _point = genericGraph.pointToScreenCoordinates(point);
					decorator.decorate(g2D, _point);
					
					if(plotSet.isxAxisMirrorEnabled()){
						Point point2 = new Point(point.getX(), -point.getY());
						_point = genericGraph.pointToScreenCoordinates(point2);
						decorator.decorate(g2D, _point);
					}
				}
			}
		}
	}
}
